package com.iotteam.androidbase.moudules.login.presenter;

import android.text.TextUtils;
import android.util.Log;

import com.iotteam.androidbase.R;
import com.iotteam.androidbase.commen.LocalInfoManager;
import com.iotteam.androidbase.commen.network.Api;
import com.iotteam.androidbase.commen.network.core.HttpListener;
import com.iotteam.androidbase.commen.network.core.HttpManager;
import com.iotteam.androidbase.commen.utils.PLog;
import com.iotteam.androidbase.commen.utils.ResUtil;
import com.iotteam.androidbase.commen.utils.ToastUtil;
import com.iotteam.androidbase.moudules.login.model.LoginInfo;
import com.iotteam.androidbase.moudules.login.view.ILoginView;

import java.util.HashMap;

/**
 * Created by lidechen on 5/3/17.
 */

public class LoginPresenterCompl implements ILoginPresener {

    private static final String TAG = "LoginPresenterCompl";

    private ILoginView mView;
    private LoginInfo mLoginInfo;

    private HashMap<String, String> mLoginParam;

    public LoginPresenterCompl(ILoginView view){
        mView = view;
        mLoginInfo = new LoginInfo();
    }

    @Override
    public void doLogin() {

        String mobile = mView.getMobile();
        String password = mView.getPassword();

        if(TextUtils.isEmpty(mobile)){
            mView.showError(ResUtil.getStringById(R.string.dialog_mobileerr));
            return;
        }
        if(TextUtils.isEmpty(password)){
            mView.showError(ResUtil.getStringById(R.string.dialog_pwderr));
            return;
        }
        if(password.length() < 6 || password.length() > 16){
            mView.showError(ResUtil.getStringById(R.string.dialog_pwdshorterr));
            return;
        }

        mLoginParam = new HashMap<>();
        mLoginParam.put("mobile", mobile);
        mLoginParam.put("password", password);

        HttpManager.getInstance().postBean(Api.login, mLoginParam, LoginInfo.class, new HttpListener<LoginInfo>() {
            @Override
            public void onSucceed(int what, LoginInfo bean) {

                if(bean.getCode() == 0) {
                    ToastUtil.showLong(R.string.label_activity_login_success);
                    //ToastUtil.showLong(bean.toString());
                    //save user info
                    LocalInfoManager.getInstance().saveUserInfo(bean);

                    PLog.i(TAG, LocalInfoManager.getInstance().getUserInfo().toString());

                    mView.finishView();
                }else{
                    if(!TextUtils.isEmpty(bean.getMessage())) {
                        ToastUtil.showLong(bean.getMessage());
                    }else{
                        ToastUtil.showLong(R.string.label_activity_login_fail);
                    }
                }
            }

            @Override
            public void onFailed(int what, Exception e) {
                ToastUtil.showShort(R.string.label_activity_login_fail);
            }

            @Override
            public void onStart(int what) {

            }

            @Override
            public void onFinish(int what) {

            }
        });
    }

    @Override
    public void cancelDoLogin() {

    }
}
